<?php

// 引入配置文件
include_once('config/config.php');


// 查询LoginAdmin是否存在 存在则赋值给变量（转为数组格式）
$LoginAdmin = isset($_COOKIE['LoginAdmin']) ? json_decode($_COOKIE['LoginAdmin'], true) : [];

if($LoginAdmin){
    $id = $LoginAdmin['id'] ?? 0 ;
    $username = $LoginAdmin['username'] ?? '' ;
   
    $sql = "SELECT * FROM `pre_admin` WHERE `id` = '$id' AND `username` = '$username'";
    $admin = find($sql);
    
    if(!$admin){
        // 清除cooki 
        setcookie('LoginAdmin', '', time() - 1);
        showMsg('非法登录','login.php');
    }
    showMsg('无需重复登录','index.php');
}



// 判断是否有POST数据提交
if ($_POST) {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $code = $_POST['code'] ?? '';
    
    // 判断参数是否为空
    if(empty($username))
    {
        showMsg('请输入用户名');
    }

    if(empty($password))
    {
        showMsg('请输入密码');
    }

    if(empty($code))
    {
        showMsg('请输入验证码');
    }


    if(strcasecmp($code, $_SESSION['code']) != 0){
        // 每一次判断之后清除session中的验证码
        unset($_SESSION['code']);
        showMsg('验证码错误');
    }
    

    $admin = find("SELECT * FROM  `pre_admin` WHERE `username` = '$username'");
    
    if(!$admin){
        showMsg('用户名不存在');
    }

    // 判断密码是否正确
    $password = md5($password . $admin['salt']);

    if( $password != $admin['password']){
        showMsg('密码错误');
    }

    // 存储登录信息
    $data = [
        'id' => $admin['id'],
        'username' => $admin['username'],
        'avatar' => $admin['avatar'],
    ];

    // 设置cookie 
    // 设置cookie的过期时间为7天
    setcookie('LoginAdmin', json_encode($data), time() + 3600 * 24);

    showMsg('登录成功','index.php');
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <?php include_once("common/meta.php"); ?>
  </head>


  <body> 

    
    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.html"><span class="second">Admin</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                <form method="post">
                        <label>用户名</label>
                        <input type="text" placeholder="请输入用户名" name="username" class="span12">
                        <label>密码</label>
                        <input type="password" placeholder="请输入密码" name="password" class="span12">

                        <!-- 验证码 -->
                        <label>验证码</label>
                        <input type="text" name="code" placeholder="请输入验证码" class="span5">
                        <img src="/config/imagescode.php" onclick="this.src = '/config/imagescode.php?id='+Math.random()" alt="">


                        <!-- 登录按钮 -->
                        <button type="submit" class="btn btn-primary pull-right">登录</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

  
    
  </body>
</html>
<?php  include_once("common/script.php");?>
<script>
    // 登录验证
    $('button[type="submit"]').click(function(){
        if(!$.trim($('input[name="username"]').val())){
            alert('用户名不能为空');
            return false;
        }

        if(!$.trim($('input[name="password"]').val())){
            alert('密码不能为空');
            return false;
        }


        if(!$.trim($('input[name="code"]').val())){
            alert('验证码不能为空');
            return false;
        }
       
      
        
    });
</script>


